package com.weilive.platform.gate.json.handler;

import org.springframework.stereotype.Service;

import com.weilive.core.netty.NettyErrors;
import com.weilive.core.protocol.json.JsonRequest;
import com.weilive.core.protocol.json.JsonRequestHandler;
import com.weilive.core.protocol.json.JsonResponse;
import com.weilive.core.utils.MD5Utils;
import com.weilive.core.utils.RegexUtils;
import com.weilive.platform.PlatformConstants;
import com.weilive.platform.PlatformErrors;
import com.weilive.platform.PlatformSecurity;
import com.weilive.platform.cache.user.UserManager;
import com.weilive.platform.cache.user.UserObj;
import com.weilive.platform.gate.json.protocol.CSA_PlatUserBind;
import com.weilive.platform.gate.json.protocol.CSQ_PlatUserBind;

/**
 * 
 * 用户绑定账号
 * 
 * @author houge
 *
 */
@Service
public class CSQ_PlatUserBind_JsonHandler extends JsonRequestHandler {

	public Class<?> getRequestType() {
		return CSQ_PlatUserBind.class;
	}

	public Class<?> getResponseType() {
		return CSA_PlatUserBind.class;
	}

	private boolean checkSign(CSQ_PlatUserBind params, String secret) throws Exception {
		long delta = System.currentTimeMillis() - params.date;
		if (delta > PlatformConstants.SERVER_SIGN_VALID_INTERVAL
				|| delta < -PlatformConstants.SERVER_SIGN_VALID_INTERVAL) {
			return false;
		}
		String account = "";
		if (params.account != null)
			account += params.account;
		if (params.email != null)
			account += params.email;
		if (params.phone != null)
			account += params.phone;
		String sign = MD5Utils.encryptToHex(secret + params.date
				+ MD5Utils.encryptToHex(params.app_id + params.uin + params.token + account + params.password));
		return params.sign.equals(sign);
	}

	public JsonResponse handleRequest(JsonRequest request) throws Exception {
		CSQ_PlatUserBind params = (CSQ_PlatUserBind) request.getData();
		if (params.date == null || params.sign == null || params.app_id == null || params.uin == null
				|| params.token == null || params.password == null
				|| (params.account == null && params.email == null && params.phone == null)) {
			return createErrorResponse(NettyErrors.BAD_REQUEST);
		}

		// 检验参数
		if (params.account != null && RegexUtils.isAccount(params.account) == false) {
			return createErrorResponse(PlatformErrors.INVALID_ACCOUNT);
		}
		if (params.email != null && RegexUtils.isEmail(params.email) == false) {
			return createErrorResponse(PlatformErrors.INVALID_EMAIL);
		}
		if (params.phone != null && RegexUtils.isPhone(params.phone) == false) {
			return createErrorResponse(PlatformErrors.INVALID_PHONE);
		}

		// 获取用户
		Long user_id = UserManager.getUserId(params.uin, params.app_id, params.token);
		if (user_id == null) {
			return createErrorResponse(PlatformErrors.INVALID_TOKEN);
		}
		UserObj user = UserManager.getUser(user_id);
		if (user == null) {
			return createErrorResponse(PlatformErrors.USER_NOT_EXIST);
		}
		// 已经绑定过了
		if (user.info.account != null || user.info.email != null || user.info.phone != null) {
			return createErrorResponse(PlatformErrors.USER_ALREADY_BIND);
		}

		// 检测签名
		if (checkSign(params, user.info.secret) == false) {
			return createErrorResponse(PlatformErrors.USER_SIGN_FAILED);
		}

		// 读取密码
		String password = PlatformSecurity.base64ServerDecrypt(params.password);
		if (password == null || RegexUtils.isPassword(password) == false) {
			return createErrorResponse(PlatformErrors.INVALID_PASSWORD);
		}

		// banding用户
		if (UserManager.bindUser(user.info.user_id, params.account, params.email, params.phone, password) == false) {
			return createErrorResponse(PlatformErrors.USER_BIND_ACCOUNT_EXIST);
		}

		CSA_PlatUserBind response = new CSA_PlatUserBind();
		return JsonResponse.create(response);
	}

}
